﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
   public partial class BsQuoteBusiness
    {
       public static bool IsQueteIDExists(string QuoteID)
        {
            bool isTag = true;
            try
            {
                string str = @"select * from BS_Quote where QuoteID=@QuoteID";
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                dpc.AddWithValue("@QuoteID", QuoteID);
                DataTable dt = DataAccessCommon.Select(str, dpc).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    isTag = true;
                }
                else
                {
                    isTag = false;
                }
            }
            catch (Exception ex)
            {
                isTag = false;
                throw ex;
            }
            return isTag;
        }

       public static bool SaveOrUpdate(BsQuoteEntity entity, DataTable QuoteDetailDT, string type)
       {
           bool istag = true;
           using (DataAccessBroker broker = DataAccessFactory.Instance())
           {
               broker.BeginTransaction();
               try
               {
                   if (type == "Save")
                   {
                       DataAccessCommon.Insert(entity, broker);
                   }
                   else if (type == "Update")
                   {
                       DataAccessCommon.UpdateOnly(entity, broker);
                   }
                   if (QuoteDetailDT != null && QuoteDetailDT.Rows.Count > 0)
                   {
                       foreach (DataRow drItem in QuoteDetailDT.Rows)
                       {
                           BsQuotedetailEntity DetailEntity = new BsQuotedetailEntity();
                           DetailEntity.QuoteID = entity.QuoteID;
                           DetailEntity.OrderNum = CommonMethod.ConvertToDecimal(drItem["OrderNum"]);
                           DetailEntity.ProductID = CommonMethod.GetString(drItem["ProductID"]);
                           DetailEntity.CustomerPartNo = CommonMethod.GetString(drItem["CustomerPartNo"]);
                           DetailEntity.Nums = CommonMethod.ConvertToDecimal(drItem["Nums"]);
                           DetailEntity.Price = CommonMethod.ConvertToDecimal(drItem["Price"]);
                           decimal amount = CommonMethod.ConvertToDecimal(drItem["Nums"]) * CommonMethod.ConvertToDecimal(drItem["Price"]);
                           DetailEntity.Amount = Math.Round(amount, 2);
                           if (entity.Currency.Equals("1"))//人民币
                           {
                               DetailEntity.Tax = Math.Round(CommonMethod.ConvertToDecimal(drItem["Tax"]) * amount);
                           }
                           else
                           {
                               DetailEntity.Tax = null;
                           }
                           string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                           string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                           if (strIsNew == "1" && strIsDel != "1")
                           {
                               DataAccessCommon.Insert(DetailEntity, true, broker);
                           }
                           if (strIsNew != "1" && strIsDel != "1")
                           {
                               DetailEntity.QuoteDetailID = CommonMethod.ConvertToDecimal(drItem["QuoteDetailID"]);
                               DataAccessCommon.UpdateOnly(DetailEntity, broker);
                           }
                           if (strIsNew != "1" && strIsDel == "1")
                           {
                               //删除明细
                               ClsCommDA.Del("BS_QuoteDetail", "QuoteDetailID", CommonMethod.GetString(drItem["QuoteDetailID"]), broker);
                           }
                       }
                   }
                   broker.Commit();
               }
               catch
               {
                   istag = false;
                   broker.Rollback();
                   throw;
               }
               return istag;
           }
       }

       public static DataTable GetDataByID(string id)
       {
           return BsQuoteDataAccess.GetDataByID(id);
       }

       public static DataTable SelectList(string QuoteID, string ProductID, string ProductName, string From, string End, int pageIndex, int PageSize, out int m_count)
       {
           return BsQuoteDataAccess.SelectList(QuoteID, ProductID, ProductName, From, End, pageIndex, PageSize, out m_count);
       }

       public static DataTable SelectDialogList(string strQuoteID, string strProductName, string OrderID, int pageIndex, int PageSize, out int m_count)
       {
           return BsQuoteDataAccess.SelectDialogList(strQuoteID, strProductName, OrderID, pageIndex, PageSize, out m_count);
       }

       public static bool Del(string strCode)
       {
           bool isResult = true;
           using (DataAccessBroker broker = DataAccessFactory.Instance())
           {
               try
               {
                   DataAccessCommon.Delete(strCode, "QuoteID", "BS_QuoteDetail");
                   DataAccessCommon.Delete(strCode, "QuoteID", "BS_Quote");
                   broker.Commit();
               }
               catch (Exception ex)
               {
                   isResult = false;
                   broker.Rollback();
                   throw ex;
               }
           }
           return isResult;
       }
    }
}
